package cn.tedu.boot42.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
   @RequestMapping("/upload")
   //MultipartFile 是文件上传的对象
    public String upload(MultipartFile picFile) throws IOException {
       System.out.println("picFile = " + picFile);
       //得到原始文件名
       String fileName = picFile.getOriginalFilename();
       //得到后缀名
       String suffix = fileName.substring(fileName.lastIndexOf("."));
       //得到唯一文件名 UUID.randomUUID，得到一个唯一标识符
       fileName = UUID.randomUUID()+suffix;
       System.out.println("文件名:"+fileName);
       //准备保存图片的文件夹路径
       String dirPath= "D:/files";
       File dirFile =new File(dirPath);
       if(!dirFile.exists()){
           dirFile.mkdirs();//创建文件夹
       }
       //得到文件的完整路径
       String filePath = dirPath+"/"+fileName;
       //把文件保存到此路路径
       picFile.transferTo(new File(filePath));
       System.out.println("文件保存完成去此路径去检查"+filePath);
     return fileName;
   }
   @RequestMapping("/remove")
   public void remove(String name){
      String dirPath= "D:/files";
      String fileName = dirPath+"/"+name;
      new File(fileName).delete();

   }
}
